/* eslint no-underscore-dangle: 0 */
import React, { Component } from 'react';


export default class CountDown extends Component {
    constructor(props) {
        super(props);


        this.state = {
            day: null, // 倒计时--天
            hour: null, // 倒计时--时
            minute: null, // 倒计时--分
            second: null, // 倒计时--秒
        };
        this.timer = null;
    }
    componentDidMount() {
        this.countFun(this.props.times);
    }
    componentWillUnmount() {
        if (this.timer) {
            clearInterval(this.timer);
            this.timer = null;
        }
    }
  countFun = (times) => {
      let remaining = times;
      this.timer = setInterval(() => {
      // 防止出现负数
          if (remaining >= 1000) {
              remaining -= 1000;
              const day = Math.floor((remaining / 1000 / 3600) / 24);
              const hour = Math.floor((remaining / 1000 / 3600) % 24);
              const minute = Math.floor((remaining / 1000 / 60) % 60);
              const second = Math.floor(remaining / 1000 % 60);
              this.setState({
                  day: day === 0 ? null : `${day}:`,
                  hour: hour < 10 ? hour === 0 ? null : `0${hour}:` : `${hour}:`,
                  minute: minute < 10 ? `0${minute}:` : `${minute}:`,
                  second: second < 10 ? `0${second}` : second,
              });
          } else {
              clearInterval(this.timer);
              // 倒计时结束时触发父组件的方法
              // this.props.timeEnd();
          }
      }, 1000);
  }


  render() {
      const {
          day, hour, minute, second,
      } = this.state;
      return (


          <span>
              { day} {hour} {minute} {second}
          </span>

      );
  }
}
